﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ByHand.Model;
using ByHand.Services.Implements;
using ByHand.Services.Interfaces;
using Newtonsoft.Json.Linq;

namespace BackEnd.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private readonly ICityService _CityService;
        private readonly IDistrictService _DistrictService;
        private readonly IUserService _UserService;

        public HomeController(ICityService CityService, IDistrictService DistrictService, IUserService UserService)
        {
            _CityService = CityService;
            _DistrictService = DistrictService;
            _UserService = UserService;

        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult City()
        {
            var cities = _CityService.All();
            foreach (var item in cities)
            {
                item.Districts = _DistrictService.GetByCity(item.ID);
            }
            return View(cities);
        }
        public ActionResult SaveCity(string name)
        {
            if (!_CityService.IsExist(name) && name != null)
            {
                City c = new City() { Name = name };
                _CityService.Add(c);
                TempData["mess"] = "Đã thêm tỉnh/thành phố " + name;
            }
            else
            {
                TempData["mess"] = "Thành phố " + name + " đã tồn tại!";
            }
            return RedirectToAction("City");
        }
        public ActionResult SaveDistrict(string name, int cityID)
        {
            District d = new District()
            {
                CityID = cityID,
                Name = name
            };
            _DistrictService.Add(d);
            TempData["mess"] = "Đã thêm quận/huyện " + name;
            return RedirectToAction("City");
        }
        public ActionResult GetRefStatistic(string time)
        {
            int totalDay = 0;
            var fromDay = GetFromDate(time, out totalDay);
            var startDay = new DateTime(fromDay.Year, fromDay.Month, fromDay.Day, 0, 0, 0);
            var userandorder = StatisticsService.GetUserAndOrder(startDay, startDay.AddDays(totalDay), Convert.ToInt16(Resources.Resource.TopUserRef));
            var domainandorder = StatisticsService.DomainAndOrder(startDay, startDay.AddDays(totalDay), Convert.ToInt16(Resources.Resource.TopUserRef));
            List<object> userStatistic = new List<object>();
            List<object> domainStatistic = new List<object>();
            foreach (int item in userandorder.Keys)
            {
                var u = _UserService.GetById(item);
                userStatistic.Add(new
                {
                    click = StatisticsService.GetClickByUser(startDay, startDay.AddDays(totalDay), item),
                    order = userandorder[item],
                    email = u.Email,
                    money = StatisticsService.MoneyByUser(startDay, startDay.AddDays(totalDay), item)
                });
            }
            foreach (string item in domainandorder.Keys)
            {
                domainStatistic.Add(new
                {
                    click = StatisticsService.GetClickByDomain(startDay, startDay.AddDays(totalDay), item),
                    order = domainandorder[item],
                    domain = item,
                    money = StatisticsService.MoneyByDomain(startDay, startDay.AddDays(totalDay), item)
                });
            }
            return Json(new { users = userStatistic, domains = domainStatistic });
        }
        public ActionResult GetStatistic(string time)
        {
            int totalDay = 0;
            var fromDay = GetFromDate(time, out totalDay);
            var startDay = new DateTime(fromDay.Year, fromDay.Month, fromDay.Day, 0, 0, 0);

            int totalclick = 0;
            int totalorder = 0;
            float totalmoney = 0;
            List<object> resultClick = new List<object>();
            List<object> resultOrderDone = new List<object>();
            for (int i = 0; i < totalDay; i++)
            {
                int click = StatisticsService.CountClickBuy(startDay.AddDays(i), startDay.AddDays(i + 1));
                int order = StatisticsService.CountOrderSuccess(startDay.AddDays(i), startDay.AddDays(i + 1));
                float money = StatisticsService.TotalMoneyOrderSuccess(startDay.AddDays(i), startDay.AddDays(i + 1));
                totalclick += click;
                totalorder += order;
                totalmoney += money;
                resultClick.Add(new
                {
                    day = startDay.AddDays(i).ToString("dd-MM"),
                    click = click
                });
                resultOrderDone.Add(new
                {
                    day = startDay.AddDays(i).ToString("dd-MM"),
                    order = order
                });
            }
            var cul = System.Globalization.CultureInfo.GetCultureInfo("vi-VN");
            return Json(new { totalclick = totalclick, totalorder = totalorder, totalmoney = totalmoney.ToString("###,#", cul.NumberFormat), dataclick = resultClick, dataorder = resultOrderDone });
        }
        private DateTime GetFromDate(string time, out int totalDay)
        {
            var cur = DateTime.Now;
            int diff = 0;
            if (time == "thisweek")
            {
                diff = cur.DayOfWeek - DayOfWeek.Monday;
                if (diff < 0)
                {
                    diff += 7;
                }
            }
            switch (time)
            {
                case "thismonth": totalDay = cur.Day; return new DateTime(cur.Year, cur.Month, 1, 0, 0, 0);
                case "7day": totalDay = 7; return cur.AddDays(-6);
                case "thisweek": totalDay = diff + 1; return cur.AddDays(-1 * diff);
                case "yesterday": totalDay = 1; return cur.AddDays(-1);
                case "today": totalDay = 1; return cur;
                default: totalDay = 0; return cur;
            }
        }
    }
}
